package createdomain2;

## This file is part of the Simplemail project.
#  Copyright (c) 2008 the Simplemail Team (see authors).
# 
#  For the full copyright and license information, please view the COPYING
#  file that was distributed with this source code. You can also view the
#  COPYING file online at http://unixforum.org.ua/misc/COPYING.txt
# 
#  @copyright  (c) the authors
#  @author     Dmitriy Shilenko <rtyug@ukr.net>
#  @license    GPLv2 http://unixforum.org.ua/misc/COPYING.txt
#  @package    Panel Mailbox
#  @version    $Id: $

use base 'createdomain', 'func';
use Data::Validate::Domain qw(is_domain);
use Data::Validate::Email qw(is_email);
use strict;
use CGI::Carp qw(fatalsToBrowser);

sub new {

#    my ( $self, %common ) sub new {
    my $self = shift->SUPER::new(@_);
    $self->dbi_connect if $self->can("dbi_connect");
    return $self;
}

sub valid_param {
    my $self = shift;

    my %he;

    if (   ( !length( $self->{p}->{fDomain} ) )
        || ( 4 > length( $self->{p}->{fDomain} ) )
        || ( 250 < length( $self->{p}->{fDomain} ) )
        || !is_domain( $self->{p}->{fDomain} ) )
    {
        $he{fDomain_correct} = 1;
    }

    if ( 200 < length( $self->{p}->{fDescription} ) ) {
        $he{fDescription_correct} = 1;
    }

    if ( $self->{p}->{fBackupmx} eq 'on' ) {
        $self->{p}->{fAliases}   = -1;
        $self->{p}->{fMailboxes} = -1;
        $self->{p}->{fMaxquota}  = -1;
        $self->{p}->{fBackupmx}  = 1;
    }
    else {
        $self->{p}->{fBackupmx} = 0;

        if ( $self->{p}->{fAliases} != -1 ) {
            if (   ( !length( $self->{p}->{fAliases} ) )
                || ( 3 < length( $self->{p}->{fAliases} ) )
                || ( $self->{p}->{fAliases} !~ /^\d+$/ ) )
            {
                $he{fAliases_correct} = 1;
            }
        }

        if (   ( !length( $self->{p}->{fMailboxes} ) )
            || ( 3 < length( $self->{p}->{fMailboxes} ) )
            || ( $self->{p}->{fMailboxes} !~ /^\d+$/ ) )
        {
            $he{fMailboxes_correct} = 1;
        }

        if (   ( !length( $self->{p}->{fMaxquota} ) )
            || ( 4 < length( $self->{p}->{fMaxquota} ) )
            || ( $self->{p}->{fMaxquota} !~ /^\d+$/ ) )
        {
            $he{fMaxquota_correct} = 1;
        }

        if (   $he{fMaxquota_correct} != 1
            && $he{fMailboxes_correct} != 1
            && $he{fAliases_correct} != 1
            && $self->{p}->{fAliases} != -1 )
        {

            $self->{p}->{fMaxquota} = $self->{p}->{fMaxquota} * 1048576;
            if ( $self->{p}->{fAliases} < $self->{p}->{fMailboxes} ) {
                $self->{p}->{fAliases} = $self->{p}->{fMailboxes};
            }
        }

    }

    my $dd;
    if (   ( $self->{session}->{name} ne 'Admin' )
        && ( $he{fDomain_correct_exist} != 1 ) )
    {

        my $sth = $self->{dbh}->prepare(
            'SELECT (select count(*) from domain where idnane = ?) AS c_domain,
                                m_domain,
                                m_mailbox,
                                m_alias,
                                mb_mailbox
                         FROM admin_domain 
                         WHERE idname = ? limit 1
                             '
        );
        $sth->execute( $self->{session}->{user}, $self->{session}->{user} );
        $dd = $sth->fetchrow_hashref();
        $sth->finish();

        if ( $dd->{m_alias} == -1 ) {
            $self->{p}->{fAliases} = $self->{p}->{fMailboxes};
        }

        if (   ( $dd->{m_mailbox} != 0 )
            && ( $dd->{m_mailbox} < $self->{p}->{fMailboxes} ) )
        {
            $he{fDomain_mailbox} = 1;
        }

        if ( $dd->{m_alias} == -1 ) {
            $self->{p}->{fAliases} = -1;
        }
        else {

            if (   ( $dd->{m_alias} != 0 )
                && ( $dd->{m_alias} < $self->{p}->{fAliases} )
                && ( $dd->{m_alias} != -1 ) )
            {
                $he{fDomain_alias} = 1;
            }

        }

        if (   ( $dd->{mb_mailbox} != 0 )
            && ( $dd->{mb_mailbox} < $self->{p}->{fMaxquota} ) )
        {
            $he{fDomain_limitmb} = 1;
        }

        if ( ( $dd->{m_mailbox} != 0 ) && ( $self->{p}->{fMailboxes} == 0 ) ) {
            $he{fDomain_no_unlim} = 1;
        }

        if ( ( $dd->{m_alias} != 0 ) && ( $self->{p}->{fAliases} == 0 ) ) {
            $he{fAliases_no_unlim} = 1;
        }

        if ( ( $dd->{mb_mailbox} != 0 ) && ( $self->{p}->{fMaxquota} == 0 ) ) {
            $he{fMaxquota_no_unlim} = 1;
        }
    }

    $he{DOMAIN} = $self->{p}->{fDomain};

    $self->{dd} = $dd;

    $self->{he} = \%he;

    return $self;

}

sub valid_param2 {
    my $self = shift;

    if (   ( $self->{dd}->{m_domain} != 0 )
        && ( $self->{dd}->{m_domain} <= $self->{dd}->{c_domain} )
        && ( $self->{session}->{name} ne 'Admin' ) )
    {
        $self->{he}->{fDomain_limit} = 1;
    }

    if ( $self->{he}->{fDomain_correct} != 1 ) {

        my $sth = $self->{dbh}->prepare(
            'SELECT domain
                         FROM domain where domain = ? limit 1  
                             '
        );
        $sth->execute( $self->{p}->{fDomain} );
        my $domain_sql = $sth->fetchrow_array();
        $sth->finish();

        if ( defined($domain_sql) ) {
            $self->{he}->{fDomain_correct_exist} = 1;

        }

    }
    return $self;

}

sub insert_domain {
    my $self = shift;

    my $fount;
    foreach my $value ( values( %{ $self->{he} } ) ) {
        if ( $value == 1 ) { $fount = 1; last; }
    }

    if ( !$fount ) {

        my $sth = $self->{dbh}->prepare(' 
                 INSERT INTO domain
              (idnane,domain,description,aliases,mailboxes,maxquota,transport,backupmx,created,modified)
              VALUES (?,?,?,?,?,?,?,?,DATETIME("NOW"),DATETIME("NOW"))
              ' );

        $sth->execute(
            $self->{session}->{user},      $self->{p}->{fDomain},
            $self->{p}->{fDescription},    $self->{p}->{fAliases},
            $self->{p}->{fMailboxes},      $self->{p}->{fMaxquota},
            $self->{se}->{transport_user}, $self->{p}->{fBackupmx}
        );
        $sth->finish();

        $self->{he}->{domain_added} = 1;
    }

    return $self;
}

sub return_create_domain {
    shift->SUPER::return_create_domain(@_);

}

sub templ_hash_domain {
    my $self = shift;

    $self->{session}->{filet} = 'create-domain';
    $self->SUPER::templ_hash(@_);

}

sub DESTROY {
    my $self = shift;
    $self->SUPER::DESTROY if $self->can("SUPER::DESTROY");
}

1;
